<?php
namespace app\index\model;

use app\common\model\SuperModel;
use think\Model;
use think\Request;

class Server extends SuperModel
{

    /**
     * 合服时间修改器
     * @param    string $value 合服时间
     */
    public function setMergeAtAttr($value)
    {
        $rtn = empty($value) ? null : $value;
        return $rtn;
    }

    /**
     * 合服对象修改器
     * @param    string $value 合服ID
     */
    public function setMergeToAttr($value)
    {
        $rtn = empty($value) ? null : $value;
        return $rtn;
    }

    /**
     * 列表(主键列表，条件数组，闭包查询)
     *
     * @param    array $param 查询条件
     * @return   void
     */
    public function doList($param = null)
    {
        try {

            //排除字段
            $result = self::where($param)
                ->select();

            $rows = [];

            foreach ($result as $data) {
                $item = $data->getData();
                array_push($rows, $item);
            }

            //返回结果
            $res = array();

            $total = count($rows);

            if ($total > 0) {
                $res['rows']  = $rows;
                $res['total'] = $total;
            } else {
                $res['total'] = 0;
                $res['rows']  = array();
            }

            //请求类型返回
            return ($res);

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }
    /**
     * 列表(主键列表，条件数组，闭包查询)
     *
     * @param    array $param 查询条件
     * @return   void
     */
    public function doServerNameList($param = null)
    {

        //查询数据
        $result = self::field('server_id,server_name')
            ->where($param)
            ->select();

        $rows = [];

        foreach ($result as $data) {
            $item = $data->getData();
            array_push($rows, $item);
        }

        //返回结果
        $res = array();

        $total = count($rows);

        if ($total > 0) {
            $res['rows']  = $rows;
            $res['total'] = $total;
        } else {
            $res['total'] = 0;
            $res['rows']  = array();
        }

        //请求类型返回
        return ($res);
    }
    
    /**
     * 批量操作
     */
    public function doBatUpdate(Request $request)
    {

        try {

            //根据对象主键,更新数据
            $param  = $request->param();
            $arr_id = explode(",", $param['id']);

            $list = [];

            foreach ($arr_id as $id) {
                array_push($list, ['id' => $id, 'status' => $param['status']]);
            }

            self::isUpdate(true)->saveAll($list);

            //返回删除结果
            //
            $output['msg']     = '更新成功';
            $output['success'] = true;
            $output['data']    = $list;

            return ($output);

        } catch (\Exception $e) {

            $output['msg']     = $e->getMessage();
            $output['success'] = false;

            return ($output);
        }
    }
    /**
     * 查询开服日期时间
     *
     * @return   [string] 开服日期时间
     */
    public function getOpenDateTime()
    {
        # 记录开服日期的服务器
        #
        $server = self::getByServerId(1);
        return ($server->open_date);
    }

    /**
     * 查询开服日期
     *
     * @return   [string] 开服日期
     */
    public function getOpenDate()
    {
        $datetime = $this->getOpenDateTime();
        return substr($datetime, 0, 10);
    }
}
